package com.yiku.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.yiku.auto.entity.CboOperators;
import com.yiku.auto.entity.CompleteCompleteapplydocline;
import com.yiku.auto.entity.Insurancercvline;
import com.yiku.auto.entity.PmRcvline;
import com.yiku.auto.mapper.CboOperatorsMapper;
import com.yiku.auto.mapper.CompleteCompleteapplydoclineMapper;
import com.yiku.auto.mapper.InsurancercvlineMapper;
import com.yiku.auto.mapper.PmRcvlineMapper;
import com.yiku.common.APIResultDTO;
import com.yiku.common.U9CloudDTO;
import com.yiku.service.U9CloudService;
import com.yiku.utils.HttpRequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


/**
 * @Author:ludada
 * @Date:2024/02/22 09:46
 */
@Service
public class U9CloudServiceImpl implements U9CloudService {
    Logger log =  LoggerFactory.getLogger(U9CloudServiceImpl.class);


    @Autowired
    U9CloudDTO u9CloudDTO;

//    @Autowired
//    InsurancercvlineMapper shLineMapper;
    @Autowired
    PmRcvlineMapper shLineMapper;

    @Autowired
    CompleteCompleteapplydoclineMapper wgLineMapper;

    @Autowired
    CboOperatorsMapper operatorsMapper;




    @Cacheable(value = "token")
    @Override
    public APIResultDTO<String> getToken() {
        APIResultDTO<String> result = new APIResultDTO<>();
        String url = u9CloudDTO.getUrl() + "/webapi/OAuth2/AuthLogin";
        String u9CToken = HttpRequestUtil.getU9CToken(url, u9CloudDTO);
        if (u9CToken != null) {
            JSONObject jsonObject = JSONObject.parseObject(u9CToken);
            if (jsonObject != null && jsonObject.getBoolean("Success")) {
                String data = jsonObject.getString("Data");
                result.setFlag(true);
                result.setData(data
                );
            }
        }

        return result;
    }
        private String getZjCodeByName(String name ){
        String result = "";
        switch (name){
            case "合格入库":
                result = "01";
            break;
            case "未达标可以入库":
                result = "02";
                break;
            case "不合格退回":
                result = "03";
                break;
        }
        return result;
        }
    @Override
    public APIResultDTO<String> updateU9CTable(Map<String,Object> forms) {
        APIResultDTO<String> result = new APIResultDTO<>();
        String data = "";
        String srcType = (String) forms.getOrDefault("来源类别",null);
        String docNo = (String) forms.getOrDefault("单号",null);

        switch (srcType){
            case "标准收货":
            case "委外收货":
                //更新收货行
                List details = JSONArray.parseArray((String) forms.getOrDefault("标准收货、委外收货明细",null));
                if(!CollectionUtils.isEmpty(details)){
                    for (Object detail : details) {
                        PmRcvline line = new PmRcvline();
                        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(detail));
                        JSONArray rowValue = jsonObject.getJSONArray("rowValue");
                        if(!CollectionUtils.isEmpty(rowValue)){
                            for (Object o : rowValue) {
                                JSONObject js = (JSONObject)o;
                                if(js.getString("label").equals("子表主键")){
                                    line.setId(js.getLong("value"));
                                }
                                  if(js.getString("label").equals("质检情况")){
                                    line.setDescflexsegmentsPrivatedescseg1(js.getString("value"));
                                }
                                  if(js.getString("label").equals("质检后续处理")){
                                    line.setDescflexsegmentsPrivatedescseg2(getZjCodeByName(js.getString("value")));
                                }
                            }
                        }
                        int update = shLineMapper.updateById(line);
                        if(update>0){
                            data += "单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexsegmentsPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexsegmentsPrivatedescseg2()+"更新成功";
                            log.info("单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexsegmentsPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexsegmentsPrivatedescseg2()+"更新成功");
                        }else{
                            data += "单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexsegmentsPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexsegmentsPrivatedescseg2()+"更新失败";
                            log.info("单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexsegmentsPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexsegmentsPrivatedescseg2()+"更新失败");
                        }


                    }
                }




                break;
            case "完工申报":
                //更新收货行
                List details1 = JSONArray.parseArray((String) forms.getOrDefault("完工申报明细",null));
                if(!CollectionUtils.isEmpty(details1)){
                    for (Object detail : details1) {
                        CompleteCompleteapplydocline line = new CompleteCompleteapplydocline();
                        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(detail));
                        JSONArray rowValue = jsonObject.getJSONArray("rowValue");
                        if(!CollectionUtils.isEmpty(rowValue)){
                            for (Object o : rowValue) {
                                JSONObject js = (JSONObject)o;
                                if(js.getString("label").equals("子表主键")){
                                    line.setId(js.getLong("value"));
                                }
                                if(js.getString("label").equals("质检情况")){
                                    line.setDescflexfieldPrivatedescseg1(js.getString("value"));
                                }
                                if(js.getString("label").equals("质检后续处理")){
                                    line.setDescflexfieldPrivatedescseg2(getZjCodeByName(js.getString("value")));
                                }
                            }
                        }
                        int update = wgLineMapper.updateById(line);
                        if(update>0){
                            data += "单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexfieldPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexfieldPrivatedescseg2()+"更新成功";
                            log.info("单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexfieldPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexfieldPrivatedescseg2()+"更新成功");
                        }else{
                            data += "单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexfieldPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexfieldPrivatedescseg2()+"更新失败";
                            log.info("单号："+docNo+"==>子表："+line.getId()+" 质检情况："+line.getDescflexfieldPrivatedescseg1()+" 质检后续处理："
                                    +line.getDescflexfieldPrivatedescseg2()+"更新失败");
                        }


                    }
                }

                break;

        }
        result.setFlag(true);
        result.setData(data);
        return result;
    }

    @Override
    public String getYwyCodeByCreateByName(String tjrgh) {
        MPJQueryWrapper<CboOperators> qw = new MPJQueryWrapper<>();
        qw.innerJoin("CBO_Operators_trl a on t.id=a.id");
        qw.select("top 1 t.code")
                .eq("a.name",tjrgh);
        return operatorsMapper.selectJoinOne(String.class, qw);

    }


}
